package svc

import (
	"center-data-platform/app/content/api/internal/config"
	"center-data-platform/app/content/rpc/client/contentservice"
	"github.com/zeromicro/go-zero/zrpc"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

type ServiceContext struct {
	Config     config.Config
	ContentRpc contentservice.ContentService
	DB         *gorm.DB
}

func NewServiceContext(c config.Config) *ServiceContext {
	gormDB, err := gorm.Open(mysql.Open(c.Mysql.Datasource), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		panic(err)
	}
	return &ServiceContext{
		Config:     c,
		ContentRpc: contentservice.NewContentService(zrpc.MustNewClient(c.ContentRpc)),
		DB:         gormDB,
	}
}
